package com.bjfu.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bjfu.common_utils.R;
import com.bjfu.eduservice.entity.EduCourse;
import com.bjfu.eduservice.entity.EduTeacher;
import com.bjfu.eduservice.service.EduCourseService;
import com.bjfu.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/eduservice/indexfront")
//@CrossOrigin
@Api("课程首页展示")
public class IndexFrontController {
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduTeacherService teacherService;

    // 查询前8条热门课程记录，查询前4名名师
    @Cacheable(value = "Course&Teacher",key = "'selectIndexList'")
    @GetMapping("index")
    public R index(){
        // 查询前8门课程
        QueryWrapper<EduCourse> courseWrapper = new QueryWrapper<>();
        courseWrapper.orderByDesc("id");
        courseWrapper.last(" limit 8");
        List<EduCourse> courseList = courseService.list(courseWrapper);
        // 查询前4名名师
        QueryWrapper<EduTeacher> teacherWrapper = new QueryWrapper<>();
        teacherWrapper.orderByDesc("id");
        teacherWrapper.last(" limit 4");
        List<EduTeacher> teacherList = teacherService.list(teacherWrapper);
        return R.ok().data("courseList",courseList).data("teacherList",teacherList);
    }

}
